package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryLowerLevelMessageLocationReq;
import com.sg.dto.base.component.req.BatchQueryMessagePositionReq;
import com.sg.dto.base.component.req.QueryMessagePositionDetailReq;
import com.sg.dto.base.component.req.QueryMessagePositionListReq;
import com.sg.entity.OmsMessageLocation;
import com.sg.service.base.component.MOmsMessageLocationService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageLocationMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageLocationServiceImpl
        extends ServiceImpl<OmsMessageLocationMapper, OmsMessageLocation>
        implements MOmsMessageLocationService {

    @Autowired
    OmsMessageLocationMapper omsMessageLocationMapper;

    /**
     * 2-5-05批量查询消息位置[4591]
     * gen by moon at 12/1/2023, 5:08:12 PM
     */
    @Trace(operationName = "2-5-05批量查询消息位置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocation> batchQueryMessagePosition(BatchQueryMessagePositionReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocation> queryWrapper = new QueryWrapper<>();

            if (reqDto.getMessagePositionList() == null || reqDto.getMessagePositionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("location_code", reqDto.getMessagePositionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocation.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocation.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocation.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            // TODO: 2023/6/12 特殊排序，手写代码
            queryWrapper.orderByDesc(CommonFunctionHelper.buildSpecOderBy("location_code"));
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsMessageLocation>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-05查询消息位置详情[6766]
     * gen by moon at 5/16/2023, 6:36:54 PM
     */
    @Trace(operationName = "2-5-05查询消息位置详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessageLocation queryMessagePositionDetail(QueryMessagePositionDetailReq reqDto) {

        try {

            QueryWrapper<OmsMessageLocation> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageLocation> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessageLocation retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-05新增消息位置[6763]
     * gen by moon at 5/17/2023, 3:40:49 AM
     */
    @Trace(operationName = "2-5-05新增消息位置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addMessagePosition(OmsMessageLocation omsMessageLocation) {
        omsMessageLocation.setMessageLocationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessageLocation.setIsArchive("FALSE");
        omsMessageLocation.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsMessageLocation.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsMessageLocation != null && omsMessageLocation.getSubjectLifeCycle() != null && omsMessageLocation.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageLocation.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsMessageLocation.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsMessageLocation.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsMessageLocation != null && omsMessageLocation.getSubjectLifeCycle() != null && omsMessageLocation.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageLocation.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsMessageLocation.setIsValid("FALSE");
        omsMessageLocation.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsMessageLocation.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsMessageLocation)) {
            return omsMessageLocation.getMessageLocationId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-05查消息位置列表[6783]
     * gen by moon at 5/16/2023, 8:51:08 PM
     */
    @Trace(operationName = "2-5-05查消息位置列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocation> queryMessagePositionList(QueryMessagePositionListReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocation> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocation.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocation.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocation.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocation>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-05删除消息位置[6794]
     * gen by moon at 5/16/2023, 8:51:09 PM
     */
    @Trace(operationName = "2-5-05删除消息位置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMessagePosition(String messageLocationId) {

        return super.removeById(messageLocationId);
    }

    /**
     * 2-5-05修改消息位置[6799]
     * gen by moon at 5/16/2023, 8:51:10 PM
     */
    @Trace(operationName = "2-5-05修改消息位置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMessagePosition(OmsMessageLocation omsMessageLocation) {

        return super.updateById(omsMessageLocation);
    }

    /**
     * 2-5-05批量查询下级消息位置[8080]
     * gen by moon at 12/5/2023, 12:03:39 AM
     */
    @Trace(operationName = "2-5-05批量查询下级消息位置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageLocation> batchQueryLowerLevelMessageLocation(BatchQueryLowerLevelMessageLocationReq reqDto) {

        try {
            QueryWrapper<OmsMessageLocation> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageLocation.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageLocation.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageLocation.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageLocation>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

